package com.pzhu.model.help;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.pzhu.common.constant.ErrorCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 页面响应entity
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ResponseResult extends HashMap<String, Object>{


    private static final int OK_CODE = 200;
    private static final int ERR_CODE = 500;

    private static final String OK_MSG = "SUCCESS";
    private static final String ERR_MSG = "未知异常，请联系管理员";

    private static final long serialVersionUID = 1L;

    public ResponseResult() {
        put("code", OK_CODE);
        put("msg", OK_MSG);
    }

    public static ResponseResult error() {
        return error(ERR_CODE, ERR_MSG);
    }

    public static ResponseResult error(String msg) {
        return error(ERR_CODE, msg);
    }

    public static ResponseResult error(int code, String msg) {
        ResponseResult r = new ResponseResult();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static ResponseResult error(ErrorCode errorCode) {
        ResponseResult r = new ResponseResult();
        r.put("code", errorCode.getCode());
        r.put("msg", errorCode.getMsg());
        return r;
    }

    public static ResponseResult ok(String msg) {
        ResponseResult r = new ResponseResult();
        r.put("msg", msg);
        return r;
    }

    public static ResponseResult ok(Map<String, Object> map) {
        ResponseResult r = new ResponseResult();
        r.putAll(map);
        return r;
    }

    public static ResponseResult ok() {
        return new ResponseResult();
    }

    @Override
    public ResponseResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
